package com.ruoyi.book.controller;

import com.ruoyi.book.domain.Book;
import com.ruoyi.book.domain.BorrowInfo;
import com.ruoyi.book.service.IBookService;
import com.ruoyi.book.service.IBorrowService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 书籍借阅Controller
 *
 * @author yaohui
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/book/borrow")
public class BorrowController extends BaseController {
    @Autowired
    private IBorrowService borrowService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IBookService bookService;

    /**
     * 查询可借阅处理列表
     */
//    @PreAuthorize("@ss.hasPermi('book:borrow:list')")
    @PostMapping("/list")
    public TableDataInfo list(Book book) {
        startPage();
        List<Book> list = bookService.selectShareBookList(book);
        return getDataTable(list);
    }
    /**
     * 获取借阅处理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('book:borrow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        System.out.println(id);
        Book book = bookService.selectShareBookById(id);
        return success(book);
    }
    /**
     * 借阅一本图书
     *
     * @param num 书本编号
     * @return 结果
     */
//    @PreAuthorize("@ss.hasPermi('book:borrow:add')")
    @PostMapping("/{num}")
    public AjaxResult borrowOne(@PathVariable("num") String num) {
        //通过springscruit得到用户的全部信息
        String username = getUsername();
        SysUser sysUser = userService.selectUserByUserName(username);
        //通过num得到书本信息
        Book book = bookService.selectBookByNum(num);
        //判断用户是否在平台黑名单内

        //根据借阅规则判断用户以借阅本书是否超出限制以及免费时长

        //添加借阅记录,书本状态修改为借出
        Date now = DateUtils.getNowDate();
        BorrowInfo borrowInfo = BorrowInfo.builder().bookNum(num).userId(sysUser.getUserId()).borrowDate(now).closeDate(DateUtils.addDays(now, 30)).build();
        borrowService.insertBorrow(borrowInfo);
        book.setState("3");
        bookService.updateBook(book);
        //借阅成功
        return AjaxResult.success();
    }
    /**
     * 还一本书
     */
    @PostMapping("/return/{num}")
    public AjaxResult returnOne(@PathVariable("num") String num) {
        // 将书籍的状态改为归还中
        borrowService.returnOneBook(num);
        return success();
    }
    /**
     * 确认借阅者归还
     */
    @PostMapping("/confirm/{num}")
    public AjaxResult confirm(@PathVariable("num") String num) {
        // 书籍状态改为已归还
        borrowService.confirmReturnOneBook(num);
        return success();
    }
}
